// pages/jiucanhuibaokidlist/jiucanhuibaokidlist.js
const app = getApp()
var util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentdate: util.formatDate(new Date()),
    mid: null,
    classid: null,
    kidlistno: [],
    kidlistyes: [],
    addTime: null,
    name: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var mid = options.mid
    var classid = options.classid
    var name = options.name
    this.setData({
      mid: parseInt(mid),
      classid: parseInt(classid),
      name: name
    })
    wx.setNavigationBarTitle({
      title: this.data.name + "的就餐汇报" //页面标题为路由参数
    })
    console.log("mid===" + mid)
    console.log("classid===" + classid)
    this.queryData()
  },

  queryData: function () {

    var that = this;
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "classid": that.data.classid,
      "mid": that.data.mid,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "mobileSerialnumber": ""
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "get_mealrecordKidList2"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {


          that.setData({
            kidlistno: res.data.kidlistno,
            kidlistyes: res.data.kidlistyes,
            addTime: res.data.addTime
          })

        } else {
          wx.showToast({
            title: '获取数据失败',
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '获取数据失败',
          icon: 'none',
          duration: 1000,
          mask: true
        })
      },
      complete: function (res) {
        console.log('submit complete');
      }
    })
  },

  setParam: function () {
    var that = this;
    var kidListyes = []
    for (var i = 0; i < this.data.kidlistyes.length; i++) {
      kidListyes.push({
        "UserID": this.data.kidlistyes[i].UserID
      })
    }
    var kidListno = []
    for (var i = 0; i < this.data.kidlistno.length; i++) {
      kidListno.push({
        "UserID": this.data.kidlistno[i].UserID
      })
    }
    var data = {
      "userid": app.globalData.userId,
      "schoolid": app.globalData.schoolId,
      "classid": that.data.classid,
      "mid": that.data.mid,
      "appType": app.globalData.appType,
      "appKey": app.globalData.appKey,
      "timeSpan": app.globalData.timespan,
      "kidListyes": kidListyes,
      "kidListno": kidListno,
      "mobileSerialnumber": "",
    }
    wx.request({
      url: app.globalData.serverUrl,
      data: {
        "data": JSON.stringify(data),
        'act': "send_mealrecord2"
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8'
      },
      success: function (res) {
        console.log(res);
        if (res.data.status == 0) {
          wx.showToast({
            title: '发送成功',
            icon: 'none',
            duration: 1000,
            mask: true
          })
          that.queryData()
        } else {
          wx.showToast({
            title: '发送失败',
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      },
      fail: function (res) {},
      complete: function (res) {
        console.log('submit complete');
      }
    })

  },
  btnyesclick: function (e) {
    var index = e.currentTarget.dataset.index;
    var kid = this.data.kidlistyes[index];
    var kidlistno = this.data.kidlistno;
    var kidlistyes = this.data.kidlistyes;
    var temp = []
    for (var i = 0; i < kidlistyes.length; i++) {
      if (i != index) {
        temp.push(kidlistyes[i])
      }
    }
    kidlistno.push(kid)
    this.setData({
      kidlistno: kidlistno,
      kidlistyes: temp
    })
  },

  btnnoclick: function (e) {
    var index = e.currentTarget.dataset.index;
    var kid = this.data.kidlistno[index];
    var kidlistno = this.data.kidlistno;
    var kidlistyes = this.data.kidlistyes;

    var temp = []
    for (var i = 0; i < kidlistno.length; i++) {
      if (i != index) {
        temp.push(kidlistno[i])
      }

    }

    kidlistyes.push(kid)
    this.setData({
      kidlistno: temp,
      kidlistyes: kidlistyes
    })
  },

  detailclick: function (e) {
    var year = parseInt(this.data.currentdate.substring(0, 4))
    var month = parseInt(this.data.currentdate.substring(5, 7))
    wx.navigateTo({
      url: '../jiucanhuibaomonth/jiucanhuibaomonth?mid=' + this.data.mid + "&classid=" + this.data.classid + "&year=" + year + "&month=" + month+"&name="+this.data.name
    })
  },

  saveclick: function (e) {
    this.setParam()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})